package com.project.controller;

import com.project.bean.ShopBean;
import com.project.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.Map;

/**
 * 销售车应用控制器
 */
@RestController
@RequestMapping("sellShop")
public class SellShopController {
    @Autowired
    private IShopService shopService;


    /**
     * 从销售车中移除商品
     * @param productId 商品ID
     */
    @RequestMapping("removeSell")
    public String remove(String productId,HttpServletRequest request){
        //从session中取出销售车
        Map<String,ShopBean> sellMap = (Map<String,ShopBean>)request.getSession()
                .getAttribute("sellMap");
        shopService.remove(productId,sellMap);
        return "ok";
    }



    /**
     * 添加销售商品
     * @param productId 操作
     */
    @RequestMapping("addSellProduct")
    public String addSellProduct(String productId,HttpServletRequest request){
        //从session中，取出销售车
        Map<String,ShopBean> sellMap = (Map<String,ShopBean>)request.getSession()
                .getAttribute("sellMap");
        shopService.sell(productId,sellMap);

        return "ok";
    }
    /**
     * 修改销售车信息
     * @param productId 销售商品
     * @param sellNumber 销售数量
     */
    @RequestMapping("updateSellInfo")
    public String updateSellInfo(String productId,Integer sellNumber,HttpServletRequest request){
        //从session中，取出销售车
        Map<String,ShopBean> sellMap = (Map<String,ShopBean> )request.getSession().
                getAttribute("sellMap");
        shopService.updateSellInfo(productId,sellNumber,sellMap);
        return "ok";
    }

    /**
     * 得到销售车信息
     * @return 采购车集合
     */
    @RequestMapping("getSellList")
    public Collection<ShopBean> getSellList(HttpServletRequest request){
        //从session中，取出销售车
        Map<String,ShopBean> sellMap = (Map<String,ShopBean> )request.getSession().
                getAttribute("sellMap");
        return sellMap.values();
    }
}
